package com.meow.action.admin.visa;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.meow.dao.entity.basic.Users;
import com.meow.dao.entity.visa.Continent;
import com.meow.dao.mapper.visa.ContinentDao;
import com.meow.util.AjaxOut;
import com.meow.util.EntityReflect;
import com.meow.util.ExtLimit;

@Controller
@RequestMapping("/admin/visa/ContinentAction/")
public class ContinentAction {
	
	private static final long serialVersionUID = -5729742261047803024L;
	
	@Autowired
	private ContinentDao continentDao;
	private Logger logger = Logger.getLogger(ContinentAction.class);
	
	/**
	 * 查询
	 */
	@RequestMapping("search.action")
	public void search(HttpServletRequest request, HttpServletResponse response) {
		try {
			Continent continentEty = (Continent) EntityReflect.createObjectFromRequest(request, Continent.class);
			ExtLimit limit = (ExtLimit) EntityReflect.createObjectFromRequest(request, ExtLimit.class);
			continentEty.setExtLimit(limit);
			int count = continentDao.selectLimitCount(continentEty);
			List list = continentDao.selectByLimit(continentEty);
			AjaxOut.responseJSonGrid(response, list, count, new SimpleDateFormat("yyyy-MM-dd"));
		}
		catch (Exception e) {
			logger.error("", e);
		}
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("save.action")
	public void save(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			Continent continentEty = (Continent) EntityReflect.createObjectFromRequest(request, Continent.class);
			
			Users userEty = (Users) request.getSession().getAttribute("UserEty");
			if(continentEty.getId() == null) {
				continentEty.setCreateTime(new Date());
				continentEty.setCreateUser(userEty.getRealName());
				continentDao.insert(continentEty);
			}
			else {
				continentEty.setModifyTime(new Date());
				continentEty.setModifyUser(userEty.getRealName());
				continentDao.updateById(continentEty);
			}
			obj.put("result","success");
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("delete.action")
	public void delete(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String continentId = request.getParameter("continentId");
			continentDao.deleteById(Integer.parseInt(continentId));
			obj.put("result","success");
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("info",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	/**
	 * 查看详细
	 */
	@RequestMapping("getDetailInfo.action")
	public void getDetailInfo(HttpServletRequest request, HttpServletResponse response) {
		JSONObject obj = new JSONObject();
		obj.put("success",true);
		try {
			String continentId = request.getParameter("continentId");
			Continent continentEty = (Continent) continentDao.selectById(Integer.parseInt(continentId));
			obj.put("data", continentEty);
		}
		catch (Exception e) {
			logger.error("", e);
			obj.put("result","error");
			obj.put("result",e.getMessage());
		}
		AjaxOut.responseText(response, obj.toString());
	}
	
	@RequestMapping("getContinents.action")
	public void getContinents(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<Continent> list = continentDao.selectByEntity(null);
			StringBuffer sb = new StringBuffer();
			sb.append("[");
			for(int i = 0; i < list.size(); i++) {
				Continent ety = list.get(i);
				sb.append("['" + ety.getId() + "','" + ety.getName()  + "']");
				if(i < list.size() - 1) 
					sb.append(",");
			}
			sb.append("]");
			AjaxOut.responseText(response, sb.toString());
		}
		catch (Exception e) {
			logger.error("", e);
		}
	}
}
